package cn.com.fattiger.framework.cms.admin.common.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.fattiger.framework.cms.admin.common.service.DynamicQueryService;

/**
 * Created by Administrator on 2016/3/7.
 */
@Controller
public class DynamicQueryController {

    @Autowired
    DynamicQueryService dynamicQueryService;

    @RequestMapping(value = "dynamicQueryForJS.do")
    @ResponseBody
    public Object dynamicQueryForJS(@RequestBody HashMap<String, Object> data) {
        Map<String, Object> result = new HashMap<>();
        try {
            Assert.notNull(data, "data cant be null!");
            Assert.notNull(data.get("queryId"), "data cant be null!");
            Integer queryId = (Integer) data.get("queryId");

            Map<String, Object> queryResult = dynamicQueryService.query(queryId, data);

            result.put("code", 200);
            result.put("message", "success.");
            result.put("responseData", queryResult);

        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", e.getMessage());
        }

        return result;
    }

    @RequestMapping(value = "dynamicQuery.do")
    public String dynamicQuery(HttpServletRequest request, ModelMap map) {
        String template = null;// can be default template.
        Map<String, Object> data = new HashMap<>();
        Map<String, String[]> params = request.getParameterMap();
        for (Map.Entry<String, String[]> entry : params.entrySet()) {
            data.put(entry.getKey(), entry.getValue()[0]);
        }

        try {
            Assert.notNull(data, "data cant be null!");
            Assert.notNull(data.get("queryId"), "queryId cant be null!");
            Assert.notNull(data.get("template"), "template cant be null!");

            template = data.get("template").toString();
            Integer queryId = Integer.valueOf(data.get("queryId").toString());

            if (!data.containsKey("page")) {
                data.put("page", 0);
            } else {
            	 String page = (String)data.get("page");
            	 data.put("page", (null == page) ? 0 : Integer.parseInt(page));
            }
            if (!data.containsKey("pageSize")) {
                data.put("pageSize", 10);
            }else{
            	String pageSize = (String)data.get("pageSize");
           	 	data.put("pageSize", (null == pageSize) ? 10 : Integer.parseInt(pageSize));
            }

            Map<String, Object> queryResult = dynamicQueryService.query(queryId, data);

            map.put("code", 200);
            map.put("message", "success.");
            map.put("responseData", queryResult);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("message", e.getMessage());
        }

        return template;
    }

    @RequestMapping(value = "dynamicQueryGet.do")
    public String dynamicQueryGet(@RequestParam(required = true) Integer queryId,
                                  @RequestParam(required = true) String template,
                                  @RequestParam(required = false) String page,
                                  @RequestParam(required = false) String pageSize,
                                  ModelMap map) {
        try {
            Assert.notNull(queryId, "queryId cant be null!");
            Assert.notNull(template, "template cant be null!");

            Map<String, Object> data = new HashMap<>();
            data.put("template", template);
            data.put("queryId", queryId);
            data.put("page", (null == page) ? 0 : Integer.parseInt(page));
            data.put("pageSize", (null == pageSize) ? 10 : Integer.parseInt(pageSize));

            Map<String, Object> queryResult = dynamicQueryService.query(queryId, data);

            map.put("code", 200);
            map.put("message", "success.");
            map.put("responseData", queryResult);

        } catch (Exception e) {
            map.put("code", 500);
            map.put("message", e.getMessage());
        }

        return template;
    }

    @RequestMapping(value = "refreshTemplate.do")
    @ResponseBody
    public Object refreshTemplate(Integer queryId) {
        Map<String, Object> result = new HashMap<>();
        try {
            dynamicQueryService.refreshTemplate(queryId);
            result.put("code", 200);
            result.put("message", "success.");
            result.put("data", "query template load success.[" + (queryId == null ? "all" : queryId) + "]");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
